<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="pr-1 m-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleFhPrint({})" v-auth="'sa:fh:u8WmsSoDispatchs:edit'">
          <Icon icon="ant-design:printer-outlined" /> {{ t('任务单打印') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ autoid: record.autoid })">
          {{ record.cdlcode }}
        </a>
      </template>
    </BasicTable>
    <!-- <InputForm @register="registerDrawer" @success="handleSuccess" /> -->
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSaFhU8WmsSoDispatchsList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    u8WmsSoDispatchsDelete,
    u8WmsSoDispatchsListData,
  } from '/@/api/wms/sa/fh/u8WmsSoDispatchs';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  // import InputForm from './form.vue';
  import { useModal } from '/@/components/Modal';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('sa.fh.u8WmsSoDispatchs');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('销售发货管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('发货单号'),
        field: 'cdlcode',
        component: 'Input',
      },
      {
        label: t('行号'),
        field: 'irowno',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('批次'),
        field: 'ibatch',
        component: 'Input',
      },
      {
        label: t('仓库名称'),
        field: 'cwhname',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'ccusname',
        component: 'Input',
      },
      {
        label: t('拣货状态'),
        field: 'cbdefine8',
        component: 'Input',
      },
      {
        label: t('下架数量'),
        field: 'cbdefine9',
        component: 'Input',
      },
      {
        label: t('确认数量'),
        field: 'cbdefine10',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('发货单号'),
      dataIndex: 'cdlcode',
      key: 'a.cdlcode',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('行号'),
      dataIndex: 'irowno',
      key: 'a.irowno',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iquantity',
      key: 'a.iquantity',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发货数量'),
      dataIndex: 'foutquantity',
      key: 'a.foutquantity',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('剩余数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库类型'),
      dataIndex: 'whtype',
      key: 'a.whtype',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'wms_wh_type',
    },
    {
      title: t('批次'),
      dataIndex: 'ibatch',
      key: 'a.ibatch',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库名称'),
      dataIndex: 'cwhname',
      key: 'a.cwhname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'ccusname',
      key: 'a.ccusname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('拣货状态'),
      dataIndex: 'cbdefine8',
      key: 'a.cbdefine8',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('下架数量'),
      dataIndex: 'cbdefine9',
      key: 'a.cbdefine9',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('确认数量'),
      dataIndex: 'cbdefine10',
      key: 'a.cbdefine10',
      sorter: true,
      width: 130,
      align: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑销售发货'),
        onClick: handleForm.bind(this, { autoid: record.autoid }),
        auth: 'sa:fh:u8WmsSoDispatchs:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除销售发货'),
        popConfirm: {
          title: t('是否确认删除销售发货'),
          confirm: handleDelete.bind(this, { autoid: record.autoid }),
        },
        auth: 'sa:fh:u8WmsSoDispatchs:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: u8WmsSoDispatchsListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await u8WmsSoDispatchsDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 任务单打印
  async function handleFhPrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.hid;
    });
    let params = {
      ids: idsArr.join(','),
      title: '销售发货任务单打印',
      height: '0.9',
      width: '0.9',
      fileName: '销售发货任务单',
    };
    openPrintModal(true, params);
  }

  function handleSuccess() {
    reload();
  }
</script>
